﻿using System;
using System.Data;
using System.Data.Common;

/// <summary>
/// 数据访问层,最低层的数据访问对像;
/// </summary>
/// 
namespace QzEbook.Common
{
    public static class DataAccess
    {
        static DataAccess()
        {
        }
        /// <summary>
        /// 创建命令对像;
        /// </summary>
        /// <returns>返回通用数据操作对像</returns>
        public static DbCommand CreateCommand()
        {
            string dataProviderName = Config.ProviderName;
            string connectionString = Config.ConnectionString;

            DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connectionString;

            DbCommand command = conn.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            return command;
        }


        #region 用SQL语句来完成的一些操作;

        private static string clearWord(string sql)
        {
            return sql.Replace("'"," ");
        }
        /// <summary>
        /// 返回所有符合对像;
        /// </summary>
        /// <param name="sql">语句</param>
        /// <returns></returns>
        public static DataTable ExcuteSelectCommand(string sql)
        {
            DbCommand command = DataAccess.CreateCommand();
            command.CommandText = sql;
            command.CommandType = CommandType.Text;

            return ExcuteSelectCommand(command);
        }

        /// <summary>
        /// 返回受影响行数;
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(String sql)
        {
            DbCommand command = DataAccess.CreateCommand();
            command.CommandText = sql;
            command.CommandType = CommandType.Text;
            return ExecuteNonQuery(command);
        }


        public static string ExcuteScalar(String sql)
        {
            DbCommand command = DataAccess.CreateCommand();
            command.CommandText = sql;
            command.CommandType = CommandType.Text;
            return ExcuteScalar(command);
        }

        #endregion


        #region 靠存储过程来完成的一些操作;

        /// <summary>
        /// 返回所有符合对像;
        /// </summary>
        /// <param name="command">数据操作对像</param>
        /// <returns></returns>
        public static DataTable ExcuteSelectCommand(DbCommand command)
        {
            DataTable table = null;
            try
            {
                command.Connection.Open();
                DbDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);
                reader.Close();
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw ex;
            }
            finally
            {
                command.Connection.Close();
            }
            return table;
        }

        /// <summary>
        /// 只返回受影响行数;
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(DbCommand command)
        {
            int i = -1; ;
            try
            {
                command.Connection.Open();
                i = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw ex;
            }
            finally
            {
                command.Connection.Close();
            }
            return i;
        }

        /// <summary>
        /// 返回数据第一行第一列的值;
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public static string ExcuteScalar(DbCommand command)
        {
            string value = String.Empty;
            try
            {
                command.Connection.Open();
                value = command.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
                Utilities.LogError(ex);
                throw ex;
            }
            finally
            {
                command.Connection.Close();
            }
            return value;
        }

        #endregion

    }
}